package game.event;

import game.Game;
import game.Player;

import java.text.DateFormat;
import java.util.Date;


/**
 * A single event in the application.  Time data is saved
 * to allow accurate replay of events in order.
 * @author smurray
 *
 */
public abstract class Event<T> {
		
	private long timestamp = System.currentTimeMillis();
	private T event;
	private Player originator;
	
	public Event(Player originator, T event){
		this.originator = originator;
		this.event = event;
	}
	
	/**
	 * Normalizes the time of this event as a duration 
	 * (delta time).  This should be done as
	 * a step when saving this as a part of a log to make
	 * replay logic simpler.
	 *
	 * <p>old time = absolute timestamp</p>
	 * <p>after this function:</br>
	 * new time = nextTime - old time
	 * @param nextTime the previous event in the log
	 */
	public void normalizeTS(long nextTime){
		if(nextTime >= timestamp){
			timestamp = nextTime - timestamp;
		}else
			throw new IllegalArgumentException("Timestamp ("+timestamp+") is not compatible with given stamp: "+nextTime);
	}
	
	public long getTime(){
		return timestamp;
	}
	
	protected String getFormattedTime() {
		Date tempDate = new Date(getTime());
		return DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.MEDIUM).format(tempDate);
	}
	
	public T getEvent(){
		return event;
	}
	
	public Player getOriginator(){
		return originator;
	}
	
	public abstract void perform(Game game);
}